//
//  UserCareerExpCell.m
//  CoolTennisBall
//
//  Created by Coollang on 16/9/6.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "UserCareerExpCell.h"
#import "UserCareerExpModel.h"
#import "UIView+Frame.h"
#import "NSString+Extension.h"

@interface UserCareerExpCell ()
@property (weak, nonatomic) IBOutlet UIView *leftDotView;
@property (weak, nonatomic) IBOutlet UIView *VlineView;
@property (weak, nonatomic) IBOutlet UILabel *yearLabel;
@property (weak, nonatomic) IBOutlet UIView *subDeatilview;
@property (weak, nonatomic) IBOutlet UIView *topLineview;
@property (weak, nonatomic) IBOutlet UIView *sectionSepTopLine;


@property (nonatomic, strong) CAGradientLayer *gradientLayer;


@property (nonatomic, strong) NSMutableArray *subDeatilLabels;

@end

@implementation UserCareerExpCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.leftDotView.layer.cornerRadius = self.leftDotView.width * 0.5;
    self.leftDotView.layer.masksToBounds = YES;
    
    self.leftDotView.layer.borderColor = kLineBackgroundColor.CGColor;
    self.leftDotView.layer.borderWidth = 1;

}
- (void)CreatGradientLayer {
    
    if (self.gradientLayer) {
        [self.gradientLayer removeFromSuperlayer];
    }
    // 创建一个渐变色Layer
    [self.VlineView layoutIfNeeded];
    
    CAGradientLayer *gradientLayer = [[CAGradientLayer alloc] init];
    gradientLayer.frame = self.VlineView.bounds;
    
    gradientLayer.colors = @[(__bridge id)[UIColor lightGrayColor].CGColor, (__bridge id)[UIColor whiteColor].CGColor];
    
    [gradientLayer setStartPoint:CGPointMake(0.5, 0)];
    [gradientLayer setEndPoint:CGPointMake(0.5, 1)];
    [self.VlineView.layer addSublayer:gradientLayer];
    self.gradientLayer = gradientLayer;
    gradientLayer.hidden = !self.isLastLine;

}

- (void)layoutSubviews {
    [super layoutSubviews];
    //最后一个cell创建一个渐变了layer
    [self CreatGradientLayer];

}

- (void)setExpModel:(UserExpSameYearModel *)expModel {
    _expModel = expModel;
    
    self.yearLabel.text = expModel.year;
    __weak typeof(self) weakSelf = self;
    
    [self.subDeatilview.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    __block CGFloat lastLabelMaxY = 0;
    [expModel.careerArr enumerateObjectsUsingBlock:^(UserCareerExpModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UILabel *subLabel = nil;
        if (weakSelf.subDeatilLabels.count > idx) {
           subLabel = weakSelf.subDeatilLabels[idx];
        }else {
           subLabel = [self creatSubLabel];
        }
        [weakSelf.subDeatilview addSubview:subLabel];
        subLabel.text = obj.Career;
        [subLabel sizeToFit];
        CGFloat labelY = lastLabelMaxY;
        CGSize textSize = [obj.Career textSizeWithtextMaxSize:CGSizeMake(kYFScreenWidth - 53, MAXFLOAT) font:[UIFont systemFontOfSize:16]];
        subLabel.frame = CGRectMake(0, labelY, textSize.width, textSize.height);
        lastLabelMaxY = CGRectGetMaxY(subLabel.frame) + 16;
    }];
    
}

// 创建label
- (UILabel *)creatSubLabel {
    
    UILabel *label = [[UILabel alloc] init];
    label.textColor = [UIColor colorWithHex:0x787878];
    label.font = [UIFont systemFontOfSize:16];
    label.numberOfLines = 0;
    [self.subDeatilLabels addObject:label];
    return label;
}

- (NSMutableArray *)subDeatilLabels {
    if (_subDeatilLabels == nil) {
        _subDeatilLabels = [NSMutableArray array];
    }
    return _subDeatilLabels;
}


- (void)showTopLineview:(BOOL)isShow {
    self.topLineview.hidden = !isShow;
    self.sectionSepTopLine.hidden = isShow;
}

@end
